package com.dstation.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.AdministratorInfo;
import com.dstation.domain.OrderInfo;
import com.dstation.domain.VideoInfo;
import com.dstation.domain.VideoReviewLogInfo;
import com.dstation.service.interf.IVideoExamineService;
import com.dstation.service.interf.IVideoService;
import com.dstation.utils.IntelligenceExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/videoExamine")
public class VideoExamineController {

    @Autowired
    IVideoExamineService videoExampleService;

    @Autowired
    IVideoService videoService;

    @RequestMapping("/videoExaminePage")
    public String videoExaminePage(@RequestParam(value = "page")Integer page, ModelMap modelMap) {
        page=page==null?1:page;
        IPage<VideoInfo> videoInfoIPage = videoService.findAllNotAccessVideoInfo(new Page<VideoInfo>(page,10));
        System.out.println(videoInfoIPage.getRecords());
        modelMap.put("videoInfoIPage", videoInfoIPage);
        return "admin/videoExamine";
    }

    /**
     * 开启线程提交到智能审核
     * @param videoId
     * @return
     */
    @RequestMapping("/intelligenceExamine")
    @ResponseBody
    public JSONObject intelligenceExamine(@RequestParam("videoId") long videoId) {
        //1.根据examineId查询视频信息
        VideoInfo videoInfo = videoService.findVideoInfoById(videoId);
        //2.检测视频
        JSONObject jsonObject = null;   //提交结果
        try {
            jsonObject = IntelligenceExamine.uploadExamine(videoInfo.getVideoURL(), new Long(videoInfo.getVideoDuration()).intValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //3. 根据提交的结果检测视频
        JSONArray dataArr = jsonObject.getJSONArray("data");
        JSONObject data1 = (JSONObject) dataArr.get(0);
        JSONObject uploadRes = new JSONObject();
        String taskId = (String) data1.get("taskId");
        uploadRes.put("taskId", taskId);
        return uploadRes;
    }

    /**
     * 根据查询结果更新数据库即可
     * @param taskId
     * @return
     */
    @RequestMapping("/referExamineResByTaskId")
    @ResponseBody
    public JSONObject referExamineResByTaskId(@RequestParam("videoId")int videoId ,@RequestParam("taskId")String taskId) {
        //1. 得到检测结果
        JSONObject jsonObject = null;
        try {
            jsonObject =  IntelligenceExamine.referExmaineRes(taskId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //2. 查看是否通过
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        JSONObject data1 = (JSONObject) jsonArray.get(0);
        JSONArray  results = data1.getJSONArray("results");
        JSONObject passRes = new JSONObject();
        if(results == null) {

            passRes.put("res", "processing");   //正在处理中
            return passRes;
        } else {
            JSONObject result1 = (JSONObject) results.get(0);
            JSONObject result2 = (JSONObject) results.get(1);
            if((((String)result1.get("suggestion")).equals("pass")) && (((String)result2.get("suggestion")).equals("pass"))){
                passRes.put("res", "pass");
            } else {
                passRes.put("res", "deny");
            }
            return passRes;
        }
    }

    /**
     * 人工通过/拒绝通过视频审核
     * @param videoId
     * @param isPass
     * @return
     */
    @RequestMapping("accessExamine")
    @ResponseBody
    public JSONObject accessExamine(@RequestParam("id")long videoId, @RequestParam("isPass")boolean isPass,
                                    HttpSession session){
        //1. 首先更新视频状态信息
        JSONObject jsonObject = new JSONObject();
        boolean modifyRes = videoExampleService.alterVideoStatusByVideoId(videoId, isPass);
        if(modifyRes == true) {
            jsonObject.put("res", true);
        } else {
            jsonObject.put("res", false);
        }
        //2.插入一条检测记录
        VideoReviewLogInfo videoReviewLogInfo = new VideoReviewLogInfo();
        videoReviewLogInfo.setAdminId(((AdministratorInfo)session.getAttribute("adminInfo")).getAdminId());
        videoReviewLogInfo.setVideoId(videoId);
        videoReviewLogInfo.setCreateDate(new Timestamp(System.currentTimeMillis()));
        boolean saveRes = videoExampleService.addVideoReviewLogInfo(videoReviewLogInfo);
        if(saveRes == true) {
            jsonObject.put("res", true);
        } else {
            jsonObject.put("res", false);
        }
        return jsonObject;
    }

}
